<?php
namespace app\home\controller;

use app\common\logic\UserWalletGold;
use app\common\logic\UserWalletSilver as UserWalletSilverLogic;
use app\common\component\user\UserService;
use app\common\component\user_vip\UserVipService;
use app\common\component\user_wallet\UserWalletService;
use app\common\component\user_wallet_log\UserWalletLogService;
use app\common\component\user_wallet_freeze\UserWalletFreezeModel;

class Vip extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

    public function index() {
    	$resultUserVip = (new UserVipService) -> clearVip();

		// vip
    	$resultUserVip['data']['user_vip'] -> countRemainingDays();
    	$this -> view -> assign('user_vip', $resultUserVip['data']['user_vip']);
		
		// 金钱钱包
    	$resultUserWallet = (new UserWalletService) -> getUserWallet();
    	$this -> view -> assign('user_wallet', $resultUserWallet['data']['user_wallet']);

		// 冻结钱包
		$userWalletFreezeWhere = 'user_id = ' . \think\Session::get('user_id') . ' AND  money_type = 1 AND is_freeze = 0';
		$countMoneyFreeze = UserWalletFreezeModel::where($userWalletFreezeWhere) -> sum('money');
		$countInterestFreeze = UserWalletFreezeModel::where($userWalletFreezeWhere) -> sum('interest');
		$this -> view -> assign('countFreeze', $countMoneyFreeze + $countInterestFreeze);

    	$resultUserWalletLogs = (new UserWalletLogService()) -> getUserWalletLogs();
    	$this -> view -> assign('user_wallet_logs', $resultUserWalletLogs);

    	$currentUserWalletGold = (new UserWalletGold()) -> getInterest();
		$this -> view -> assign('currentUserWalletGold', $currentUserWalletGold['data']['user_wallet_gold']);

		$currentUserWalletSilver = (new UserWalletSilverLogic()) -> getInterest();
		$this -> view -> assign('currentUserWalletSilverPrice', $currentUserWalletSilver['data']['price']);

		$resultUserWalletVip = (new \app\common\component\user_wallet_vip\UserWalletVipService()) -> getUserWalletVipV01();
      	$this -> view -> assign('user_wallet_vip', $resultUserWalletVip['data']['user_wallet_vip']);

    	return $this -> view -> fetch();
    }

	public function details() {
		$resultUserWalletVip = (new \app\common\component\user_wallet_vip\UserWalletVipService()) -> getUserWalletVipV01();
      	$this -> view -> assign('user_wallet_vip', $resultUserWalletVip['data']['user_wallet_vip']);

		$this -> view -> assign('UserWalletVipLogs', \app\common\component\user_wallet_vip_log\UserWalletVipLogModel::where('user_id', \think\Session::get('user_id')) -> order('is_state ASC, user_wallet_vip_log_id DESC') -> paginate(10, true));

		return $this -> view -> fetch();
	}
	
    public function topUp() {
    	$resultUser = (new UserService) -> getUser();
    	$this -> view -> assign('user', $resultUser['data']['user']);

      	$resultUserWalletVip = (new \app\common\component\user_wallet_vip\UserWalletVipService()) -> getUserWalletVipV01();
      	$this -> view -> assign('user_wallet_vip', $resultUserWalletVip['data']['user_wallet_vip']);

		$this -> view -> assign('VIP_EVERY_MONTH_MONEY', getParam('VIP_EVERY_MONTH_MONEY'));
    	return $this -> view -> fetch();
    }

	public function topUpVip() {
		$post = $this -> request -> post();

		$resultUserWalletVip = (new \app\common\component\user_wallet_vip_log\UserWalletVipLogService()) -> createLogV01(1, 1, $post['price'], 0, uploadFile());
		if($resultUserWalletVip['code'] != 0) {
			echo '<meta charset="utf-8"><script>alert("' . $resultUserWalletVip['message'] . '");window.history.go(-1);</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("' . $resultUserWalletVip['message'] . '");window.location.href="/user/vip/details";</script>';exit;
	}

	public function freezeList() {
		$userWalletFreezeWhere = 'user_id = ' . \think\Session::get('user_id') . ' AND  money_type = 1 AND is_freeze = 0';
		$countMoneyFreeze = UserWalletFreezeModel::where($userWalletFreezeWhere) -> sum('money');
		$countInterestFreeze = UserWalletFreezeModel::where($userWalletFreezeWhere) -> sum('interest');
		$this -> view -> assign('countFreeze', $countMoneyFreeze + $countInterestFreeze);

		$this -> view -> assign('userWalletFreezes', UserWalletFreezeModel::where('user_id', \think\Session::get('user_id')) -> order('is_freeze ASC, user_wallet_freeze_id DESC') -> paginate(10, true));

		return $this -> view -> fetch();
	}
}